<?php 

global $appId;
global $secret;
global $url;
$appId = "7438873193";       //应用id
$secret = "1dc4d84aeeb9698c29c57202485581b5";  //应用密钥
$url = "https://smlopenapi.esign.cn";    // 沙箱环境

/**
* 生成13位时间戳  字符串签名用
*/
function getMillisecond() {
    list($t1, $t2) = explode(' ', microtime());
    return (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000);
}
// 将body数据md5加密
function changeMd5($data=array()){
    return base64_encode(md5(json_encode($data),true));
}
/**
 * 生成签名
 * @param $httpMethod
 * @param $accept
 * @param $contentType
 * @param $contentMd5
 * @param $date
 * @param $headers
 * @param $url
 * @return string
 */
function getSignature($httpMethod,$accept,$contentType,$contentMd5,$date,$headers,$url,$secret){
    $stringToSign = $httpMethod . "\n" . $accept . "\n"  . $contentMd5 . "\n" . $contentType . "\n"  . $date . "\n" .$headers  ;
        if($headers != ''){
            $stringToSign .= "\n".$url;
        }else{
            $stringToSign .= $url;
        }
    $signature = hash_hmac("sha256",utf8_encode($stringToSign), utf8_encode($secret), true);
    $signature = base64_encode($signature);
    return $signature;
}

// 获取请求头
function getHeader($data,$httpMethod,$url,$appId,$secret){
    $contentMd5 = changeMd5($data);

    $httpMethod = $httpMethod;
    $accept = "*/*";
    $contentType = "application/json; charset=UTF-8";
    $contentMd5 = $contentMd5;
    $date = "";
    $headers = "";
    $url = $url;
    $reqSignature = getSignature($httpMethod,$accept,$contentType,$contentMd5,$date,$headers,$url,$secret);
    $header = array(
        'X-Tsign-Open-App-Id:'.$appId,
        'X-Tsign-Open-Ca-Timestamp:'.getMillisecond(),
        'Accept:*/*',
        'X-Tsign-Open-Ca-Signature:'.$reqSignature,
        'Content-MD5:'.$contentMd5,
        'Content-Type:'.'application/json; charset=UTF-8',
        'X-Tsign-Open-Auth-Mode:Signature'  
    );
    return $header;
}

//常规请求类
function sendHttp($reqType='GET', $url, $headers=array(), $param=null)
{
    $connectTimeout = 30;//30 second
    $readTimeout = 80;//80 second

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $reqType);

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FAILONERROR, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $postData = is_array($param) ? json_encode($param, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : $param;
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    if ($readTimeout) {
        curl_setopt($ch, CURLOPT_TIMEOUT, $readTimeout);
    }
    if ($connectTimeout) {
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connectTimeout);
    }
    //https request
    if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    }
    if (is_array($headers) && 0 < count($headers)) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    }
    $curlRes = curl_exec($ch);

    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return $curlRes;
}





// delete 有用
function curlData($url, $data, $header, $method = 'GET', $type='json')
{
    //初始化
    $ch = curl_init();
    if ($type=='json') {
    $headers = ['Content-Type: application/json; charset=utf-8'];
    } elseif ($type=='form-urlencoded') {
    $headers = ['Content-Type: application/x-www-form-urlencoded; charset=utf-8'];
    }
    $headers=array_merge($headers, $header);
    if ($method == 'GET' && $type!='json') {
    if ($data) {
    $querystring = http_build_query($data);
    $url = $url.'?'.$querystring;
    }
    }
    // 请求头，可以传数组
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 执行后不直接打印出来
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_REFERER, "http://www.baidu.com");
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    if ($method == 'POST' && $type=='json') {
    // curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'POST'); // 请求方式
    curl_setopt($ch, CURLOPT_POST, 1); // post提交
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // post的变量
    } elseif ($method == 'POST' && $type!='json') {
    curl_setopt($ch, CURLOPT_POST, 1); // post提交
    $querystring = http_build_query($data);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $querystring); // post的变量
    }
    if ($method == 'PUT') {
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    if ($method == 'DELETE') {
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
    if (!empty($cookie)) {
    curl_setopt($ch, CURLOPT_COOKIE, $cookie);
    } //设置cookie
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 不从证书中检查SSL加密算法是否存在
    $output = curl_exec($ch); //执行并获取HTML文档内容
    $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch); //释放curl句柄
    return $output;
}

 ?>